import React, { Component, Fragment } from "react";
// antd
import { Modal, Button, Col, Pagination, Form, Input, message, Radio, Tag } from "antd";
import Action from "@c/buttonAction/Index";
// http
import http from '../../../utils/request';
import ProTable from '@ant-design/pro-table';


const { TextArea } = Input;


class RobotCustomerManage extends Component {
    constructor(props){
        super(props);
        this.state = {
            // 表格分页
            total: '',
            page: 1,
            page_number: 10,
            dataSource: [],
            columns: [
                {title: "标题", dataIndex: "name", key: "name", width: "250px",
                    render: (text, rowData) => {
                        return (
                            <Tag key={rowData.name} color="#87d068">{rowData.name}</Tag>
                        )
                    }
                },
                {title: "内容", dataIndex: "content", key: "content", ellipsis: true, copyable: true},
                {title: "消息类型", dataIndex: "answer_type", key: "answer_type", width: "100px"},
                {title: "创建人", dataIndex: "owner", key: "owner", width: "200px",
                    render: (text, rowData) => {
                        return (
                            <Tag key={rowData.id} color="blue">{rowData.owner}</Tag>
                        )
                    }
                },
                {title: "操作", dataIndex: "operation", key: "operation", width: "200px",
                    render: (text, rowData) => {
                        return (
                            <div className="inline-button">
                                <Action>
                                    <Action.Button onClick={() => this.onEditRobotCustomerTestMsgBtn({rowData: rowData})}>消息测试</Action.Button>
                                    <Action.Button onClick={() => this.onEditRobotCustomerBtn({rowData: rowData, status: true})}>编辑</Action.Button>
                                    <Action.Button onClick={() => this.onDeleteRobotCustomerBtn(rowData)}>删除</Action.Button>
                                </Action>
                            </div>

                        )
                    }           
                }
            ],
            loading: false,
            visible_add: false,     // 添加
            visible_edit: false,    // 编辑
            visible_delete: false,  // 删除
            visible_edit_id: '',
            visible_edit_name: '',
            visable_edit_code: '',
            robot_customer_id: '',
            cloud_platform_name: '',
            search_cloud_platform_name: '',
            answer_type: 'markdown',
        }
        this.formRefAdd = React.createRef();
        this.formRefEdit = React.createRef();
        this.formRefMsg = React.createRef();
    }

    componentDidMount() {
        this.loadData()
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'name__contains': this.state.search_cloud_platform_name,
        }

        // 获取分组列表
        http.get('/robot/customer', {params: requestData}).then(response=>{
            console.log(response.data, 1212)
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    /** 切换每页数量 */
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

    /** 搜索 */
    onFinishSearch = (data) => {
        this.setState({
            page: 1,
            search_cloud_platform_name: data.name ? data.name : null,
        })
        this.loadData()
    }

    /** 添加按钮 */
    onAddRobotCustomerBtn = () => {
        http.get('/robot/customer').then(response=>{
            this.formRefAdd.current.setFieldsValue({
                answer_type: this.state.answer_type
            })
        }).catch()

        this.setState({
            visible_add: true
        })
    }

    /** 添加取消 */
    onFinishAddCancel = () => {
        this.setState({
            visible_add: false
        })
        this.formRefAdd.current.resetFields()
    }

    /** 添加提交 */
    onFinishAdd = (data) => {
        const requestData = {
            name: data.name,
            content: data.content,
            answer_type: data.answer_type,
        }
        http.post('/robot/customer', requestData).then(response=>{
            message.success(response.data.msg)
            this.setState({
                visible_add: false,
            })
            this.formRefAdd.current.resetFields()
            this.loadData()
        }).catch()
    }

    /** 编辑按钮 */
    onEditRobotCustomerBtn = (data) => {
        http.get('/robot/customer').then(response=> {
            console.log(data.rowData, 12112)
            this.formRefEdit.current.setFieldsValue({
                id: data.rowData.id,
                name: data.rowData.name,
                content: data.rowData.content,
                answer_type: data.rowData.answer_type,
            })
        })
        
        this.setState({
            visible_edit: true,
        })
    }

    /** 编辑取消 */
    onFinishEditCancel = () => {
        this.setState({
            visible_edit: false,
        })
        this.formRefEdit.current.resetFields();
    }

    /** 编辑提交 */
    onFinishEdit = (data) => {
        const requestData = {
            name: data.name,
            content: data.content,
            answer_type: data.answer_type,
        }
        console.log(typeof(data.content))
        console.log(data.content)
        http.put('/robot/customer/' + data.id, requestData).then(response=>{
            message.success(response.data.msg)
            this.setState({
                visible_edit: false
            })
            this.formRefEdit.current.resetFields();
            this.loadData()
        }).catch()
    }

    /** 删除提交 */
    handleRobotCustomerDelete = () => {
        http.delete('/robot/customer/' + this.state.robot_customer_id).then(response=>{
            message.success(response.data.msg)
            this.setState({
                visible_delete: false,
            })
            this.loadData()
        }).catch()    
    }

    /** 删除按钮 */
    onDeleteRobotCustomerBtn = (data) => {
        this.setState({
            robot_customer_id: data.id,
            cloud_platform_name: data.name,
            visible_delete: true
        })
    }

    /** 变更消息类型 */
    onChangeRobotCustomerRadioBtn = (data) => {
        this.setState({
            answer_type: data.target.value
        })
    }

    /** 消息测试 */
    onEditRobotCustomerTestMsgBtn = (data) => {
        this.setState({
            loading: true
        })
        const requestData = {
            robot_customer_id: data.rowData.id,
            content: data.rowData.content,
            owner: data.rowData.owner,
            types: 'robot_customer_msg_test',

        }
        http.patch('/robot/customer/'+ data.rowData.id, requestData).then(response=>{
            message.success(response.data.msg)
            this.setState({
                loading: false
            })
        }).catch()
    }

    render(){
        const { loading, dataSource, columns, page, total, visible_add, visible_edit, visible_delete, cloud_platform_name } = this.state
        const rowSelection = {
            onChange: this.onCheckbox
        }
        var myStyle = {margin: '20px'}

        return (
            <Fragment>

                <Form layout="inline" onFinish={this.onFinishSearch}>
                    <Form.Item label="关键字" name="name">
                        <Input placeholder="请输入标题/内容关键字" style={{width: "200px"}} />
                    </Form.Item>
                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>
                <div style={{marginTop: "30px", marginBottom: "10px"}}><Button type="primary" onClick={this.onAddRobotCustomerBtn}>添加问答条目</Button></div>

                
                {/* <Table
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    scroll={{ y: 500, x: '70vw' }}
                >
                </Table> */}

                <div>
                    <ProTable
                        pagination={false}
                        rowSelection={rowSelection} 
                        columns={columns}
                        dataSource={dataSource}
                        rowKey="key"
                        toolBarRender={false}
                        search={false}
                        loading={loading}
                    />
                </div>


                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

                <Modal
                    title="添加问答条目"
                    visible={visible_add}
                    onCancel={this.onFinishAddCancel}
                    okText="确认"
                    cancelText="取消"
                    footer={null}
                    width={900}
                >
                    <Form
                        onFinish={this.onFinishAdd}
                        ref={this.formRefAdd}
                        labelCol={{ span: 3 }}
                        wrapperCol={{ span: 14 }}
                        initialValues={{ answer_type: 'markdown'}}
                    >

                        <Form.Item label="消息标题" name="name" rules={[{ required: true}]}>
                            <Input style={{ width: '500px' }} placeholder="推送消息的标题信息"></Input>
                        </Form.Item>

                        <Form.Item label="消息格式" name="answer_type" rules={[{ required: true}]}>
                            <Radio.Group  onChange={this.onChangeRobotCustomerRadioBtn}>
                                <Radio value="text" disabled>文本消息</Radio>
                                <Radio value="markdown" checked>markdown</Radio>
                                <Radio value="card" disabled>图片消息</Radio>
                                <Radio value="link" disabled>连接消息</Radio>
                            </Radio.Group>
                        </Form.Item>    

                        <Form.Item label="消息内容" name="content" rules={[{ required: true}]}>
                            <TextArea style={{ height: 400 }} placeholder="推送消息的内容信息"></TextArea>
                        </Form.Item>

                        <Form.Item>
                            <Button htmlType="submit" type="primary">确定</Button>
                        </Form.Item>                                                               
                    </Form>           
                </Modal>

                <Modal
                    title="编辑问答"
                    visible={visible_edit}
                    onCancel={this.onFinishEditCancel}
                    okText="确认"
                    cancelText="取消"
                    footer={null}
                    width={800}
                >
                    <Form
                        onFinish={this.onFinishEdit}
                        ref={this.formRefEdit}
                        labelCol={{ span: 5 }}
                        wrapperCol={{ span: 14 }}
                    >
                        <Form.Item label="ID" name="id" hidden>
                            <Input ></Input>
                        </Form.Item>

                        <Form.Item label="消息标题" name="name" rules={[{ required: true}]}>
                            <Input></Input>
                        </Form.Item>

                        <Form.Item label="消息格式" name="answer_type" rules={[{ required: true}]}>
                            <Radio.Group onChange={this.onChangeRobotCustomerRadioBtn}>
                                <Radio value="text" disabled>文本消息</Radio>
                                <Radio value="markdown">markdown</Radio>
                                <Radio value="card" disabled>图片消息</Radio>
                                <Radio value="link" disabled>连接消息</Radio>
                            </Radio.Group>
                        </Form.Item>  


                        <Form.Item label="消息内容" name="content" rules={[{ required: true}]}>
                            <TextArea style={{ height: 400 }}></TextArea>
                        </Form.Item>  

                        <Form.Item>
                            <Button htmlType="submit" type="primary">确定</Button>
                        </Form.Item>                                                               
                    </Form>           
                </Modal>

                <Modal
                    title="删除问答条目"
                    visible={visible_delete}
                    onOk={this.handleRobotCustomerDelete}
                    onCancel={() => {this.setState({visible_delete: false, robot_customer_id: ''})}}
                    okText="确认"
                    cancelText="取消"
                >
                    <p>确定删除问答条目<span style={{color: 'red'}}> {cloud_platform_name}</span>? 删除后将无法恢复</p>
                </Modal>
            </Fragment>
        )
    }
}
export default RobotCustomerManage;